__author__ = 'zoulida'
import shelve

def getShelveData(name, fun, args):#

    #调用方法
    '''import tools.shelveTool as st
    arg = '20210101', '20210602'
    st.getShelveData('test', getEveryMonthFirstOpenday, arg )'''

    #name = dateDay + '_' +str(percentage)
    #print(name)
    #from rqalpha.const import FILEPATH
    #dirstr = FILEPATH.SHELVEDIR.value
    #filepath = dirstr + 'scanZToneDay3'
    from sdufe.Config import FILEPATH
    filepath = FILEPATH.SHELVEDIR.value
    #filepath = 'F:\\quantdata\\quantaxis'
    shelveDict = shelve.open(filepath)
    if name in shelveDict:
        listResult = shelveDict[name]
    else:
        #listResult = haveBeenGreaterThanbyOneDayCodelist(dateDay, percentage)
        listResult = fun(*args)
        if listResult is not None and len(listResult) != 0:
            shelveDict[name] = listResult
    shelveDict.close()
    return listResult

def shelve_me(fn):

    def _wrapper(*args, **kwargs):
        shelvekey = 'shelve_me'
        shelvekey += fn.__name__
        shelvekey += "".join(args)
        print(shelvekey)
        #print(args)
        return getShelveData(shelvekey, fn, args)
        #fn(*args, **kwargs)


    return _wrapper

@shelve_me
def testWrapper(key):
    print('testWrapper done')
    return key

if __name__ == '__main__':
    print(testWrapper('zld2'))